// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>

struct data
{
	int h, m, s, dir;
	int t;
	std::string name;
};

std::map<std::string, int> in_time;
std::map<std::string, int> last;
std::set<std::string> names;
std::map<std::string, int> park_time;

bool operator<(data const & a, data const & b)
{
	return a.t < b.t || (a.t == b.t && a.dir > b.dir);
}

int get_num()
{
	char c1, c2; std::cin >> c1 >> c2;
	return (c1 - '0') * 10 + c2 - '0';
}

void print_single(int x)
{
	if (x < 10) std::cout << "0";
	std::cout << x;
}

void print(int t)
{
	int h = t / 3600; t %= 3600;
	int m = t / 60; t %= 60;
	int s = t;
	print_single(h); std::cout << ":";
	print_single(m); std::cout << ":";
	print_single(s);
}

std::istream & operator>>(std::istream & is, data & a)
{
	is >> a.name;
	char ch;
	a.h = get_num(); is >> ch;
	a.m = get_num(); is >> ch;
	a.s = get_num();
	std::string s; is >> s;
	if (s == "in") a.dir = 1;
	else a.dir = -1;
	a.t = a.h * 3600 + a.m * 60 + a.s;
	return is;
}

int const maxn = 10007;
int sum[maxn];
int n, k;
std::vector<data> da;

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n >> k;
	da.resize(n);
	std::vector<std::string> all;
	int max = 0;
	for (int i = 0; i < n; i++) std::cin >> da[i];
	std::sort(da.begin(), da.end());
	for (int i = 0; i < n; i++) {
		names.insert(da[i].name);
		if (da[i].dir == 1) {
			if (in_time.find(da[i].name) == in_time.end() || in_time[da[i].name] == -1)
				in_time[da[i].name] = da[i].t;
			else {
				in_time[da[i].name] = da[i].t;
				da[last[da[i].name]].dir = 0;
			}
			last[da[i].name] = i;
		} else {
			if (in_time.find(da[i].name) == in_time.end() || in_time[da[i].name] == -1) {
				da[i].dir = 0;
				continue;
			}
			int tmp = da[i].t - in_time[da[i].name];
			park_time[da[i].name] += tmp;
		//	std::cout << da[i].name << " " << i << " " << park_time[da[i].name] << '\n';
			if (park_time[da[i].name] == max) all.push_back(da[i].name);
			else if (park_time[da[i].name] > max) {
				max = park_time[da[i].name];
				all.clear();
				all.push_back(da[i].name);
			}
			in_time[da[i].name] = -1;
		}
	}
	int last = 23 * 3600 + 59 * 60 + 59;
	for (std::set<std::string>::iterator it = names.begin(); it != names.end(); ++it) {
		if (in_time.find(*it) == in_time.end() || in_time[*it] == -1)
			continue;
		int tmp = last - in_time[*it];
		park_time[*it] += tmp;
		if (park_time[*it] == max) all.push_back(*it);
		else if (park_time[*it] > max) {
			max = park_time[*it];
			all.clear();
			all.push_back(*it);
		}
	}

	/*
	for (int i = 0; i < n; i++) {
		std::cout << da[i].name << ' ';
		print(da[i].t);
		std::cout << " " << da[i].dir << ' ' << da[i].t << '\n';
	}
	*/


	for (int i = 1; i <= n; i++)
		sum[i] = sum[i - 1] + da[i - 1].dir;
//	for (int i = 1; i <= n; i++) std::cout << sum[i] << ' '; std::cout << "\n";

	for (int i = 0; i < k; i++) {
		char ch;
		int h = get_num(); std::cin >> ch;
		int m = get_num(); std::cin >> ch;
		int s = get_num();
		int t = h * 3600 + m * 60 + s;
		int l = 0, r = n - 1;
		while (l + 1 < r) {
			int mid = (l + r) / 2;
//			std::cout << ":: " << l << ' ' << mid << " " << r << "\n";
			if (da[mid].t <= t) l = mid;
			else r = mid;
		}
		if (da[l].t > t) r = l;
		r++;
		std::cout << sum[r - 1] << '\n';
	}
	std::sort(all.begin(), all.end());
	for (int i = 0; i < (int)all.size(); i++)
		std::cout << all[i] << ' ';
	print(max);
//	std::cout << " " << max << '\n';
	std::cout << "\n";
}

